package com.ugtss.busniess.controller;


import com.ugtss.busniess.domain.Student;
import com.ugtss.busniess.service.StudentService;
import com.ugtss.common.core.controller.BaseController;
import com.ugtss.common.core.domain.AjaxResult;
import com.ugtss.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/students")
public class StudentController extends BaseController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询学生信息
     *
     * @param  student 页码，从0开始
     * @return 分页后的学生信息列表
     */
    @GetMapping("getPage")
    public TableDataInfo list(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 获取单个学生详情
     *
     * @param id 学生ID
     * @return 学生详情
     */
    @GetMapping("/{id}")
    public AjaxResult getStudentById(@PathVariable Integer id) {
        Student student = studentService.getStudentByUserId(id);
        return AjaxResult.success(student);
    }

    /**
     * 保存或更新学生信息
     *
     * @param student 学生对象
     * @return 保存或更新后的学生对象
     */
    @PostMapping
    public AjaxResult saveOrUpdateStudent(@RequestBody Student student) {
        return AjaxResult.success(studentService.saveOrUpdateStudent(student));
    }

    /**
     * 删除学生信息
     *
     * @param id 学生ID
     * @return 成功响应
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteStudent(@PathVariable Integer id) {
        return AjaxResult.success(studentService.deleteById(id));
    }
}